package com.jinlejia.erp.ui.new_house.adapter;

import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * 单行文本
 */
public class KeyValueAdapter extends YfListAdapter<Map<String,String>> {

    public KeyValueAdapter(ArrayList<Map<String,String>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_key_value_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        Iterator<Map.Entry<String, String>> iterator = mData.get(i).entrySet().iterator();  //map.entrySet()得到的是set集合，可以使用迭代器遍历
        while(iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            ((ViewHolder) viewHolder).key.setText(entry.getKey());
            if(TextUtils.isEmpty(entry.getValue())){
                ((ViewHolder) viewHolder).value.setText("暂无");
            }else{
                ((ViewHolder) viewHolder).value.setText(entry.getValue());
            }
        }
        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {

        TextView key;
        TextView value;

        public ViewHolder(View itemView) {
            super(itemView);
            key = (TextView) itemView.findViewById(R.id.id_key);
            value = (TextView) itemView.findViewById(R.id.id_value);
        }
    }


}
